<template>
    <div class="page">
        <gzg-search :config="searchConfig"
                    :showMore="true" 
                    :form="searchForm"
                    :label-width="120"
                    @search="search"></gzg-search>
        <div class="tool-wrap"> 
            <div class="item">
                <div class="label">赠送商家账号额度:</div>
                <div class="val">{{ stat_data.gift_num }}</div>
            </div> 
            <div class="item">
                <div class="label">已开通商户数:</div>
                <div class="val">{{ stat_data.open_seller_num }}</div>
            </div>         
            <el-button class="btn" :icon="Plus" type="primary" @click="openDialog">新增</el-button>
        </div>
        <div class="grid-wrap">
            <el-table style="width:100%;"
                              height="100%"
                              :border="true"
                              :data="listData.data">
                <el-table-column label="序号" type="index" width="60px" fixed="left"></el-table-column>
                <el-table-column label="商家全称" prop="company_name" width="200px" fixed="left"></el-table-column>
                <el-table-column label="商家简称" prop="short_name" min-width="100px"></el-table-column>
                <el-table-column label="服务商" prop="agent_service_name" min-width="100px"></el-table-column>
                <el-table-column label="服务商分成比例" prop="agent_service_rate" min-width="130px">
                    <template #default="props">
                        <div>{{ props.row.agent_service_rate + '%' }}</div>
                    </template>
                </el-table-column>
                <el-table-column label="区域渠道" prop="agent_partner_name" min-width="100px"></el-table-column>
                <el-table-column label="渠道分成比例" prop="agent_partner_rate" min-width="120px">
                    <template #default="props">
                        <div>{{ props.row.agent_partner_rate + '%' }}</div>
                    </template>
                </el-table-column>
                <el-table-column label="状态" prop="seller_status_desc" width="80px"></el-table-column>
                <el-table-column label="接口参数" prop="create_user" width="100px">
                    <template #default="props">
                        <div>{{  props.row.is_set_api ? '已填写' : '未填写' }}</div>
                    </template>
                </el-table-column>
                <el-table-column label="创建人" prop="create_user" width="100px"></el-table-column>
                <!-- <el-table-column label="创建时间" prop="create_at" width="180px"></el-table-column> -->
                <el-table-column label="操作" width="196px" fixed="right">
                    <template #default="props">
                        <div>
                            <el-button type="primary" @click="view(props.row)" v-if="props.row.button.view_btn" link>详情</el-button>
                            <el-button type="primary" @click="edit(props.row)" v-if="props.row.button.edit_btn" link>重新提交</el-button>


                        </div>
                    </template>
                </el-table-column>

            </el-table>
        </div>
        <div class="page-wrap">
            <el-pagination
                v-model:current-page="params.page"
                v-model:page-size="params.page_size"
                :page-sizes="[100, 200, 300, 400]"
                layout="total, prev, pager, next"
                :total="listData.total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
        </div>
        <add-form v-if="show" :drawer-show="show" :state="state" :id="id"  @refresh="refresh" @close="show = false"></add-form> 
       
    </div>
</template>
<script lang="ts" setup>
import { ref, reactive, watch, computed ,onMounted } from 'vue'
import { InfoFilled, Plus, Search, Hide, View, Close  } from '@element-plus/icons-vue'
import { ElMessageBox,ElMessage } from 'element-plus'

import addForm from './component/add-form'

import { assign } from 'lodash'

import { getAlipayShopList } from 'services/shop'

import systemStore from '@/store/modules/system'

const show = ref(false)
const id = ref()
const state = ref('add')

const searchForm = reactive({
    company_name:'',
    seller_status: '',
})

const searchConfig = reactive([{
    id:'company_name',
    label:'商户全称',
    placeholder:'请输入商户全称',
    type: 'input'
},{
    id:'seller_status',
    label:'状态',
    placeholder:'请选择状态',
    type: 'select',
    enum:'seller_status_list'
}])

const stat_data = ref({
    gift_num:'0',
    open_seller_num:'0'
})

const listData = reactive({
    total:0,
    data:<any>[]
})

const params = ref({
    company_name:'',
    seller_status:'',
    page_size:  20,
    page:  1,
})

const search = (searchParams:any) => {

    assign(params.value, searchParams)

    params.value.page = 1

    getList()
}

const handleSizeChange = (val: number) => {
    params.value.page_size = val
    params.value.page = 1
    getList();
}

const handleCurrentChange = (val:number) => {
    params.value.page = val
    getList()
} 

const refresh = () => {
    show.value = false
    params.value.page = 1
    getList()
}

const getList = () => {
    getAlipayShopList(params.value).then(res => {
        if (params.value.page == 1) {
            stat_data.value = res.stat_data || {
                gift_num:'0',
                open_seller_num:'0'
            }
        }
        listData.total = parseInt(res.total_rows)
        listData.data = res.items || []
    })
}

const openDialog = () => {
    show.value = true
    state.value = 'add'
    id.value =''
}

const view = (row:any) => {
    id.value = row.id
    show.value = true
    state.value = 'view'
}

const edit = (row:any) => {
    id.value = row.id
    show.value = true
    state.value = 'edit'
}


const audit = (row:any) => {
    id.value = row.id
    state.value = 'audit'
    show.value = true
}



const dict = computed(() => {
    console.log(dict)
    return systemStore().dict
})

onMounted(() => {
    getList()
})
</script>
<style lang="scss">
</style>./component/add-form